package com.cn.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cn.entity.Drugs;



public class DrugsDao {
	public static SessionFactory sessionFactory;
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	Session session;
	
	 public void addDrugs(Drugs drugs){
		   
		   session=sessionFactory.openSession();
		   Transaction tr=session.beginTransaction();
		   session.save(drugs);
		   tr.commit();
		   session.close();
	 }
	 public List<Drugs> selectDrugs(){
		 List<Drugs> list=new ArrayList<Drugs>();
		 session=sessionFactory.openSession();
		 String hql="from Drugs";
		 Query query=session.createQuery(hql);
		 list=query.list();		 
		 return list;
	 }
	 
	 
	 
	 
	 
	 
	 
//	 public static void main(String[] args) {
//		ApplicationContext act=new ClassPathXmlApplicationContext("applicationContext.xml");
//		sessionFactory=(SessionFactory) act.getBean("sessionFactory");
//		DrugsDao dr=new DrugsDao();
////		Drugs d=new Drugs("ss",23.1,50,"ss");
////		dr.addDrugs(d);
//		List<Drugs> list=dr.selectDrugs();
//		System.out.println(list.size());
//		System.out.println("eg");
//		
//	}

}
